<!-- 编辑弹窗 -->
<template>
    <div class="ele-body">
      <el-card>
        <ele-pro-table ref="table"  row-key="id" :columns="warehouseItemColumns"  :datasource="datasource"  show-summary :summary-method="getSummaries"  :height="tableHeight" :full-height="tableFullHeight">
          <!-- 单价 -->
          <template slot="orderNO" slot-scope="{ row }">
            <el-link type="primary" :underline="false"    @click="clickOrderNo(row)" >{{ row.orderNO }}</el-link>
          </template>
        </ele-pro-table>
      </el-card>
    </div>
  </template>
  <script>

  //初始化对像
  import { removePageTab, reloadPageTab,setPageTabTitle } from '@/utils/page-tab-util';
  import { QueryLockStockByCondition } from '@/api/inventory/stock';
  export default {
    name: 'StockLock',
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
    },
    data() {
      return {
        // 表单数据
        form: {},
        fixedHeight:true,
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: 3,
       
        productId:'',
        wareHouseId:'',
        qtyTotal:0,
        outQtyTotal:0,
        lockQtyTotal:0,
        // 表格列配置
        warehouseItemColumns: [
          {columnKey: 'index', type: 'index', width: 50, align: 'center', showOverflowTooltip: true, fixed: 'left', label:'序号',},
          {prop: 'orderNO', label: '订单号',showOverflowTooltip: true, align: 'center'},
          {prop: 'productCode', label: '产品编号',showOverflowTooltip: true, align: 'center',},
          {prop: 'productName', label: '产品名称',showOverflowTooltip: true, align: 'center',},
          {prop: 'spc', label: '规格',width: 100, showOverflowTooltip: true, align: 'center',},
          {prop: 'qty', label: '订单数量', showOverflowTooltip: true,align: 'center',},
          {prop: 'outQty', label: '出库数量', showOverflowTooltip: true,align: 'center',},
          {prop: 'lockQty', label: '锁定数量', showOverflowTooltip: true,align: 'center',},
          {prop: 'unitDesc', label: '单位',width: 100, showOverflowTooltip: true, align: 'center',},
          {prop: 'wareHouseDesc', label: '仓库', showOverflowTooltip: true, align: 'center',},
        ],
      };
    },
    computed: {
      // 表格高度
      tableHeight() {
        var fh = this.fixedHeight ? 'calc(100vh - 250px)' : void 0;
        return fh;
      },
      // 表格全屏时高度
      tableFullHeight() {
        var fh =  this.fixedHeight ? 'calc(100vh - 250px)' : null;
        return fh;
      },
    },
    //初始化数据
    mounted(){
      // this.reload();
    },
    created(){
      this.productId = this.$route.query.productId;
      this.wareHouseId = this.$route.query.wareHouseId;
      setPageTabTitle('库存锁定明细');
    },
    methods: {

      /* 表格数据源 */
      async datasource(params) {
        var conParams = {productId:'',wareHouseId:''};
        params.where.pageindex = params.page;
        params.where.pagesize = params.limit;

        //添加排序
        if(params.order.order == null || params.order.order == undefined){
          params.where.orderBys = [{ sort: "createDate", order: 1, }];
        }else{
          params.where.orderBys = [];
          params.where.orderBys.push({sort:params.sorter.column.sortable,order:params.order.order=='asc'?0:1});
        }

        if(!params.where.conditions)params.where.conditions = {};

        if(this.productId){
          conParams.productId = this.productId;
        }

        if(this.wareHouseId){
          conParams.wareHouseId = this.wareHouseId;
        }

        console.log('conParams',conParams)

        let data = await QueryLockStockByCondition(conParams);

        this.qtyTotal=data.qtyTotal;
        this.outQtyTotal=data.outQtyTotal;
        this.lockQtyTotal=data.lockQtyTotal;

        this.$nextTick(() => {
          this.$refs.table.doLayout();
        });
        
        return {
            list:data.lockStockList,
            count:data.totalCount,
        };
      },

      /* 刷新表格 */
      reload(where) {
        if(this.$refs.table){
          this.$refs.table.reload({ where: where });
        }
      },

       /* 合计 */
      getSummaries(param){
        const sums = [];
        sums[0] = '合计' ;
        const { columns, data } = param;
        columns.forEach((column, index) => {
          if(index ==5){
            sums[index] =  Number(this.qtyTotal).toFixed(4);
          }else if(index ==6){
            sums[index] =  Number(this.outQtyTotal).toFixed(4);
          }else if(index ==7){
            sums[index] =  Number(this.lockQtyTotal).toFixed(4);
          }
        });

        this.$nextTick(() => {
          this.$refs.table.doLayout();
        });
        return sums;
      },

      clickOrderNo(row){
        removePageTab({ key: '/businesshandling/purordermanager' });
        this.$nextTick(() => {
          this.$router.push({path: '/businesshandling/purordermanager', query: { "billNo": row.billNo } });
        });
      },
  },
    
    /* 监听visible属性变化 */
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data && this.data.id) {
            //赋值this.form
            this.form = this.data;
            this.isUpdate = this.data.id !=null && this.data.id !=undefined;
            this.reload();
          } 
        } 
      }
    }
  };
  </script>